import datetime
import geo.geomodel
import logging as log
import cgi
import os
import datetime
import random
import geo.geomath as geomath
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from django.utils import simplejson as json

class Area (db.Model):
    
	name = db.StringProperty(required=True)
	type = db.StringProperty(required=True)
	picture = db.StringProperty()
	tel = db.StringProperty()
	email = db.StringProperty()
	description = db.StringProperty()
	address = db.StringProperty()
	
	@staticmethod
	def Create(name1, type1, pic, tel1,email1, desc, addr,self):
		area_report = Area(name = name1, type = type1, picture=pic, tel = tel1, email = email1, description = desc, address = addr)
		area_report.put()
		self.response.out.write(str(area_report.key()))

class AddAreaHandler(webapp.RequestHandler):
   	"""
	/add_area?name=Mall of America&type=mall&pic=http://2012havanesenational.files.wordpress.com/2011/10/sign.jpg&tel=612-373-3333&email=info@mallofamerica.com&desc=Mall of America has been described as a city within a city. Along with an extensive range of retail, restaurants and entertainment, there are many unique features to Mall of America likely to be found in any community.&addr=60 East Broadway Bloomington, MN 55425
	"""
	def get(self):
	
		name1 = self.request.get("name")
		type1= self.request.get("type")
		picture1 = self.request.get("pic")
		tel1 =  self.request.get("tel")
		email1 =  self.request.get("email")
		desc1 =  self.request.get("desc")
		addr1 =  self.request.get("addr")
		
       
		if (name1 and type1):
			Area.Create(name1,type1,picture1,tel1,email1,desc1,addr1,self)
		else:
			self.response.out.write("not all params were given")
			return 
		
class GetAreaHandler(webapp.RequestHandler):
    """
	/get_area?akey=ag9kZXZ-YW5nZWxzLWRlbW9yCgsSBEFyZWEYAQw
	"""
    def get(self):
        
		akey = self.request.get("akey")
		area = Area.get(akey)
		if (area):
			json_user = {
			'name': area.name,
			'type': area.type,
			'tel': area.tel,
			'email': area.email,
			'desc': area.description,
			'addr': area.address,
			'pic' : area.picture
			}
			self.response.out.write(json.dumps(json_user))			